package com.yh.csx.crm.service;

import com.github.pagehelper.PageHelper;
import com.yh.csx.crm.core.util.BeanUtils;
import com.yh.csx.crm.dao.CustomerVisitRecordMapper;
import com.yh.csx.crm.po.CustomerVisitRecord;
import com.yh.csx.crm.qo.CustomerVisitQo;
import com.yh.csx.crm.service.request.AppCustomerVisitReq;
import com.yh.csx.crm.service.request.AppCustomerVisitSearchReq;
import com.yh.csx.crm.service.vo.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Huang Zhaoping
 * @date 2019/3/1
 */
@Service
public class CustomerVisitService {

    @Autowired
    private CustomerVisitRecordMapper visitRecordMapper;

    public List<CustomerVisitRecord> findCustomerVisitList(AppCustomerVisitSearchReq req) {
        if (req.isPageable()) {
            PageHelper.startPage(req.getPageNum(), req.getPageSize());
        }
        CustomerVisitQo qo = BeanUtils.convert(CustomerVisitQo.class, req);
        return visitRecordMapper.selectVisitRecordList(qo);
    }

    public CustomerVisitRecord findCustomerVisitRecord(Long visitId) {
        return visitRecordMapper.selectByPrimaryKey(visitId);
    }

    public void saveCustomerVisitRecord(User user, AppCustomerVisitReq req) {
        CustomerVisitRecord record = BeanUtils.convert(CustomerVisitRecord.class, req);
        record.setCreateBy(user.getUserName());
        record.setVisitPersonId(user.getUserId());
        record.setVisitPersonName(user.getUserName());
        record.setVisitImgsUrl(StringUtils.join(req.getVisitImgs(), ","));
        visitRecordMapper.insertSelective(record);
    }

    public List<CustomerVisitRecord> findCustomerLastOneForEachType(Long customerId) {
        return visitRecordMapper.selectLastOneForEachType(customerId);
    }
}
